import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:tiktokadult/hj_page/short_video/short_follow_logic.dart';
import 'package:waterfall_flow/waterfall_flow.dart';
import 'package:tiktokadult/hj_page/home_business/home_cell_style/video_simple_cell_widget.dart';
import 'package:tiktokadult/hj_utils/api_service/common_service.dart';
import 'package:tiktokadult/routers/jump_router.dart';
import 'package:tiktokadult/tools_base/debug_log.dart';
import 'package:tiktokadult/tools_base/loading/l_error_widget.dart';
import 'package:tiktokadult/tools_base/loading/loading_center_widget.dart';
import 'package:tiktokadult/tools_base/refresh/pull_refresh.dart';

import '../../../hj_model/video_model.dart';
import '../../alert_business/video/recommend_video_res.dart';
import '../../hj_model/home/home_tabs_entity.dart';

class ShortFollowPage extends StatefulWidget {


  @override
  State<StatefulWidget> createState() {
    return _ShortFollowViewState();
  }
}

class _ShortFollowViewState extends State<ShortFollowPage> {

  late ShortFollowLogic logic;


  @override
  void initState() {
    logic = ShortFollowLogic();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: logic,
        builder: (_){
      if (logic.dataArr == null) {
        return LoadingCenterWidget();
      } else if (logic.dataArr!.isEmpty) {
        return CErrorWidget(
          retryOnTap: () {
            logic.onInitData();
          },
        );
      } else {
        return pullYsRefresh(
          onLoading: (refreshController) {
            logic.onLoadMoreData();
          },
          onRefresh: (refreshController) {
            logic.onRefreshData();
          },
          onInit: (ctr) => logic.refreshController = ctr,
          child: CustomScrollView(
            slivers: [
              SliverPadding(
                padding: const EdgeInsets.fromLTRB(12, 0, 12, 0),
                sliver: SliverWaterfallFlow(
                  gridDelegate: const SliverWaterfallFlowDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 2,
                    crossAxisSpacing: 6,
                    mainAxisSpacing: 10,
                  ),
                  delegate: SliverChildBuilderDelegate(
                        (BuildContext c, int index) {
                      VideoModel videoModel = logic.dataArr![index];
                      var ratio = 168 / 266;
                      return VideoSimpleCellWidget(
                        videoModel: videoModel,
                        imgAspectRatio: ratio,
                        textLines: 1,
                        onTap: () {
                          if (videoModel.isRandomAd() == true) {
                            pushToPageByLink(videoModel.randomAdsInfo?.href, id: videoModel.randomAdsInfo?.id);
                          } else {
                            pushToVideoPage(videoModel: videoModel, videoArr: logic.dataArr);
                          }
                        },
                      );
                    },
                    childCount: logic.dataArr?.length ?? 0,
                  ),
                ),
              ),
            ],
          ),
        );
      }
    });
  }
}
